{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "editnerf.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true,
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/stevliu/editnerf/blob/master/editnerf.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "TR1l7vA4UO3q"
      },
      "source": [
        "### Instructions\n",
        "To run the code below:\n",
        "\n",
        "- Run all the cells to first load the relevant code, install the dependencies, and fetch pretrained models. \n",
        "- Finally, run the UI. Please read the UI instructions below for further instructions on how to operate the user interface. \n",
        "\n",
        "\n",
        "Feel free to check out http://editnerf.csail.mit.edu/ for additional results and details!"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AH1l-GWSTsxd"
      },
      "source": [
        "# First, clone the repo.\n",
        "!git clone https://github.com/stevliu/editnerf.git 1>> setup_log.txt\n",
        "%cd editnerf\n",
        "# Then, install the dependencies that Colab doesn't have yet.\n",
        "!pip install configargparse 1>> setup_log.txt\n",
        "%cd torchsearchsorted \n",
        "!pip install . 1>> setup_log.txt\n",
        "%cd ../"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Bx-0NcyHVvFK"
      },
      "source": [
        "!bash scripts/setup_models.sh 1>> setup_log.txt\n",
        "%cd ui"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "F1WurRgVUWH4"
      },
      "source": [
        "try: # set up path\n",
        "    import google.colab, sys, torch\n",
        "    sys.path.append('/content/editnerf')\n",
        "    if not torch.cuda.is_available():\n",
        "        print(\"Change runtime type to include a GPU.\")  \n",
        "except:\n",
        "    pass"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OBAKsUvtV-vC"
      },
      "source": [
        "## To operate the interface:\n",
        "In all edits, first pick a view to edit. Once you have constructed your editing example, click the 'execute' button to execute the edit. \n",
        "\n",
        "Particular editing examples can be saved or loaded. Please click the text box on the top right of the interface to select an edit to load. \n",
        "\n",
        "Please refer to these instructions for how to construct your own editing examples. \n",
        "\n",
        "### Color Edits: \n",
        "Click the \"edit color\" button, choose a color, and scribble the desired color change. Optionally, you may select the background brush \"BG\" and scribble where to keep unchanged. \n",
        "\n",
        "### Shape Removal: \n",
        "Click the \"remove shape\" button, scribble where you would like to remove. We treat the unmodified parts of the image as the background to keep unchanged. This can be overriden by scribbling on the canvas with the \"BG\" brush. \n",
        "\n",
        "### Color/Shape Transfer: \n",
        "Click the \"color/shape transfer\" button, and choose a instance on the bottom to transfer color/shape from. \n",
        "\n",
        "### Shape Addition:\n",
        "Click the \"add shape\" button, and select a instance to copy from on the bottommost panel. A copy interface will show up, and scribble over a region to copy from. Finally, click on the region in the editing canvas the region to paste onto. \n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qAP1aheQUaHr"
      },
      "source": [
        "import editingapp\n",
        "from ui_utils import show\n",
        "\n",
        "# This specifies which dataset to use. \n",
        "# The choices are: 'photoshapes/config.txt', 'cars/config.txt', 'dosovitskiy_chairs/config.txt'\n",
        "config_file = 'photoshapes/config.txt'\n",
        "\n",
        "# This specifies which object instance to edit. \n",
        "# You can preview which instance number corresponds to which instance using the next cell. \n",
        "# For PhotoShapes, we find instances 0, 1, 5, 7, and 11 are nice to edit. \n",
        "instance_num = 5\n",
        "\n",
        "# This specifies the number of views to display. \n",
        "# Consider reducing this number if the interface is taking too long to load/update.\n",
        "num_canvases = 3\n",
        "\n",
        "interface = editingapp.NeRFEditingApp(instance=instance_num, config=config_file, num_canvases=num_canvases)\n",
        "show(interface)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FnuKXL4YWVKs"
      },
      "source": [
        "### This is a util for previewing what the corresponding instance to an instance number looks like. \n",
        "import os\n",
        "from ui_utils import show\n",
        "from PIL import Image\n",
        "\n",
        "# This is the dataset. Choices are 'photoshapes', 'dosovitskiy_chairs', 'cars'\n",
        "dataset = 'photoshapes' \n",
        "\n",
        "# This is your instance number. \n",
        "# This visualization supports numbers in {0, ..., 20}\n",
        "instance_num = 5\n",
        "\n",
        "# Visualize an example view of the instance\n",
        "instance_path = os.path.join(dataset, 'instances', '{:03d}.png'.format(instance_num))\n",
        "show(Image.open(instance_path))"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}